# -*- coding:utf-8 -*-
# Created by Myth on 3/18/2018.
from operator import itemgetter, attrgetter


def exercise1():
    """遍历list,求和"""
    list1 = [2, 4, 6, 8, 100]
    sum = 0
    for element in list1:
        sum += element
    print(sum)


def exercise2():
    """获得list中的最大值,内建函数max,min的用法"""
    list1 = [1, 31, 5, 70, 8]
    max_item = max(list1)
    print(max_item)


def exercise3():
    """为一个 item是非空tuple的list排序，按照每个tuple的最后一个元素的大小，从小到大"""
    tuple_list = [(1, 3), (99, 2, 111), (2, 1), (9,), (0, 2, 3, 24)]
    l = sorted(tuple_list, key=lambda tup: tup[-1], reverse=False)
    # 使用operator模块
    l2 = sorted(tuple_list, key=itemgetter(-1), reverse=True)
    print(l2)


def exercise4():
    """list去重"""
    list1 = [2, 3, 3, 3, 2, 1, 22, 1, 2, 1]
    list2 = []
    for ele in list1:
        if ele not in list2:
            list2.append(ele)
    print(list2)


def exercise5():
    """给一个元素，在列表中删除之"""
    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    list1.remove(4)  # 根据内容删除,必须保证内容 在此list中
    list1.pop(4)  # 根据位置删除
    print(list1)


def exercise6():
    """key是否存在"""
    dict1 = {'key0': 0, 'key1': 1, 'key2': 2}
    if 'key0' in dict1:
        print("Exist!")
    else:
        print("Not Exist")


def exercise7():
    """取出来dict里的元素"""
    dict1 = {'key0': 0, 'key1': 1, 'key2': 2}
    items = dict1.items()
    keys = dict1.keys()
    values = dict1.values()
    print(items)
    print(keys)
    print(values)
    if ('key0', 0) in items:
        print("Yes")
    else:
        print("No")


def exercise7():
    """按key  value分别排序"""
    # key 排序
    dict1 = {'key0': 0, 'key1': 2, 'key2': 1}
    key_list = sorted(dict1)  # ['key0', 'key1', 'key2']
    # print(key_list)
    for key in key_list:
        print("{}:{}".format(key, dict1[key]))
    # value 排序
    l = sorted(dict1.items(), key=itemgetter(1))
    print(l)
exercise7()


def exercise():
    """"""
